package com.ruoyi.web.controller.app;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.system.domain.AppUser;
import com.ruoyi.system.service.IAppUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AppOrder;
import com.ruoyi.system.service.IAppOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author xuk
 * @date 2024-08-14
 */
@Controller
@RequestMapping("/app/order")
public class AppOrderController extends BaseController
{
    private String prefix = "app/order";

    @Autowired
    private IAppOrderService appOrderService;

    @Autowired
    private IAppUserService appUserService;

    @RequiresPermissions("app:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }

    /**
     * 查询用户信息列表
     */
    @RequiresPermissions("app:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppOrder appOrder)
    {
        startPage();
        List<AppOrder> list = appOrderService.selectAppOrderList(appOrder);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @RequiresPermissions("app:order:export")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AppOrder appOrder)
    {
        List<AppOrder> list = appOrderService.selectAppOrderList(appOrder);
        ExcelUtil<AppOrder> util = new ExcelUtil<AppOrder>(AppOrder.class);
        return util.exportExcel(list, "用户信息数据");
    }

    /**
     * 新增用户信息
     */
    @GetMapping("/add")
    public String add( ModelMap mmap)
    {

        List<AppUser> userList = appUserService.selectAppUserList(null);
        mmap.put("userList",userList);
        return prefix + "/add";
    }

    /**
     * 新增保存用户信息
     */
    @RequiresPermissions("app:order:add")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult addSave(AppOrder appOrder)
    {
        Long userId = appOrder.getUserId();
        if(userId!=null && userId>0){
            AppUser appUser = appUserService.selectAppUserByUserId(userId);
            BigDecimal shoppingPoints = appUser.getShoppingPoints();
            BigDecimal realPrice = appOrder.getRealPrice();
            appUser.setShoppingPoints(shoppingPoints.add(realPrice));
            appUserService.updateAppUser(appUser);
        }else{
            return error("请选择客户！");
        }

        return toAjax(appOrderService.insertAppOrder(appOrder));
    }

    /**
     * 修改用户信息
     */
    @RequiresPermissions("app:order:edit")
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        AppOrder appOrder = appOrderService.selectAppOrderByOrderId(orderId);
        mmap.put("appOrder", appOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户信息
     */
    @RequiresPermissions("app:order:edit")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppOrder appOrder)
    {
        return toAjax(appOrderService.updateAppOrder(appOrder));
    }

    /**
     * 删除用户信息
     */
    @RequiresPermissions("app:order:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(appOrderService.deleteAppOrderByOrderIds(ids));
    }
}
